package com.controller.teacher;

import com.common.paging.PagingData;
import com.dto.excution.ListExcution;
import com.dto.in.ResumeInfo;
import com.entity.*;
import com.enums.ListEnum;
import com.service.SeekTeacherService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@RequestMapping("/seekTeacher")
@RestController
public class SeekTeacherController {

    @Autowired
    private SeekTeacherService seekTeacherService;

    /**
     * 查询(筛选)老师
     */
    @ApiOperation(value = "查询(筛选)老师")
    @PostMapping(value = "/seekTeacher")
    @ResponseBody
    public ListExcution<List<ListTeacherInfo>> seekTeacher(String teacherName,String teacherSex,String cityName,Integer teacherValue,Integer orderCount, String teachingStage, String schoolId, String subjectId, String teachingAreaId, Integer limit){
        List<ListTeacherInfo> list = seekTeacherService.seekTeacher(teacherName,teacherSex,cityName,teacherValue,orderCount,teachingStage,schoolId,subjectId,teachingAreaId,limit);
        return new ListExcution<List<ListTeacherInfo>>(list, ListEnum.SUCCESS);
   }

    /**
     * 单查询老师简历
     */
    @ApiOperation(value = "单查询老师简历")
    @PostMapping(value = "/findTeacherDetail")
    @ResponseBody
    public ListExcution<Teacherinfo> findTeacherDetail(Integer userId){
        Teacherinfo teacherinfo = seekTeacherService.findTeacherDetail(userId);
        return new ListExcution<Teacherinfo>(teacherinfo, ListEnum.SUCCESS);
    }

    @ApiOperation(value = "编辑简历")
    @PostMapping(value = "/updateResume")
    @ResponseBody
    public ListExcution<Void> updateResume(HttpServletRequest request,ResumeInfo resumeInfo)throws IOException{
        request.setCharacterEncoding("UTF-8");
        MultipartHttpServletRequest multipartRequest =     (MultipartHttpServletRequest) request;
        MultipartFile video = multipartRequest.getFile("video"); // 通过参数名获取指定文件
        String path = null;
        String type = null;
        if(video == null){
            resumeInfo.setVideos("");
            int success = seekTeacherService.updateResume(resumeInfo);
            return new ListExcution<Void>(null, ListEnum.SUCCESS);
        }else{
            String fileName = video.getOriginalFilename();
            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            if (type != null) {
                if (type.equals("mp4")) {
                    // 项目在容器中实际发布运行的根路径
                    String realPath = "/usr/lyjjImage/lyjj2/video/";
//                    String realPath = "E://ssss/itemBank/";
                    // 自定义的文件名称
                    String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
                    // 设置存放图片文件的路径
                    path = realPath + trueFileName;
                    video.transferTo(new File(path));
                    resumeInfo.setVideos("https://www.auriakj.com/lyjjImage/lyjj2/video/" + trueFileName);
                    int i = seekTeacherService.updateResume(resumeInfo);
                    if (i == 1)
                        return new ListExcution<Void>(null, ListEnum.SUCCESS);
                    else return  new ListExcution<Void>(null, ListEnum.FAIL);
                } else {
                    System.out.println("不是我们想要的文件类型,请按要求重新上传");
                    return  new ListExcution<Void>(null, ListEnum.FAIL);
                }
            } else {
                System.out.println("文件类型为空");
                return  new ListExcution<Void>(null, ListEnum.FAIL);
            }
        }
    }

    @ApiOperation(value = "查询名师值和是否隐藏简历")
    @PostMapping(value = "/findValueResume")
    @ResponseBody
    public ListExcution<Teacherinfo> findValueResume(Integer userId){
        Teacherinfo info = seekTeacherService.findValueResume(userId);
        return new ListExcution<Teacherinfo>(info, ListEnum.SUCCESS);
    }

    @ApiOperation(value = "查是否第一此修改简历")
    @PostMapping(value = "/findIsOne")
    @ResponseBody
    public ListExcution findIsOne(Integer userId){
        int count = seekTeacherService.findIsOne(userId);
        return new ListExcution(count, ListEnum.SUCCESS);
    }


    //新增接口↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

//    @ApiOperation(value = "插入教师等级")
//    @PostMapping(value = "/insertGrade")
//    @ResponseBody
//    public ListExcution insertGrade(){
//        int count = seekTeacherService.insertGrade();
//        return new ListExcution(count, ListEnum.SUCCESS);
//    }


    @ApiOperation(value = "修改教师等级")
    @PostMapping(value = "/updateGrade")
    @ResponseBody
    public ListExcution<Void> updateGrade(Integer userId,Integer gradeType){
        int count = seekTeacherService.updateGrade(userId,gradeType);
        if(count > 0){
            return new ListExcution<Void>(null,ListEnum.SUCCESS);
        }else {
            return new ListExcution<Void>(null, ListEnum.FAIL);
        }
    }

    @ApiOperation(value = "修改教师教龄")
    @PostMapping(value = "/updateAge")
    @ResponseBody
    public ListExcution<Void> updateAge(Integer userId,Integer age){
        int count = seekTeacherService.updateAge(userId,age);
        if(count > 0){
            return new ListExcution<Void>(null,ListEnum.SUCCESS);
        }else {
            return new ListExcution<Void>(null, ListEnum.FAIL);
        }
    }

    @ApiOperation(value = "查询老师职位")
    @PostMapping(value = "/findPosition")
    @ResponseBody
    public ListExcution<PagingData> findPosition(Integer page, Integer length){
        page = (page-1) * 10;
        PagingData pagingData = seekTeacherService.findPosition(page,length);
        return new ListExcution<PagingData>(pagingData, ListEnum.SUCCESS);
    }

    @ApiOperation(value = "查询答疑列表")
    @PostMapping(value = "/findItemBankList")
    @ResponseBody
    public ListExcution<List<ItemBankInfo>> findItemBankList(Integer limit){
        List<ItemBankInfo> list = seekTeacherService.findItemBankList(limit);
        return new ListExcution<List<ItemBankInfo>>(list, ListEnum.SUCCESS);
    }

    @ApiOperation(value = "删除自己个儿的答疑")
    @PostMapping(value = "/deleteMyItemBank")
    @ResponseBody
    public ListExcution<Void> deleteMyItemBank(Integer itemBankId){
        int count = seekTeacherService.deleteMyItemBank(itemBankId);
        if(count > 0){
            return new ListExcution<Void>(null, ListEnum.SUCCESS);
        }else {
            return new ListExcution<Void>(null, ListEnum.FAIL);
        }
    }

    @ApiOperation(value = "删除自己个儿的反馈")
    @PostMapping(value = "/deleteMyFeedBack")
    @ResponseBody
    public ListExcution<Void> deleteMyFeedBack(Integer feedBackId){
        int count = seekTeacherService.deleteMyFeedBack(feedBackId);
        if(count > 0){
            return new ListExcution<Void>(null, ListEnum.SUCCESS);
        }else {
            return new ListExcution<Void>(null, ListEnum.FAIL);
        }
    }

    @ApiOperation(value = "查询答疑详情")
    @PostMapping(value = "/findItemBankDetail")
    @ResponseBody
    public ListExcution<ItemBankInfo> findItemBankDetail(Integer itemBankId){
        ItemBankInfo list = seekTeacherService.findItemBankDetail(itemBankId);
        return new ListExcution<ItemBankInfo>(list, ListEnum.SUCCESS);
    }


    @ApiOperation(value = "上传答疑图片")
    @PostMapping(value = "/uploadItemBank")
    @ResponseBody
    public synchronized String uploadItemBank(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        request.setCharacterEncoding("UTF-8");
        String user = request.getParameter("userId");
        String content = request.getParameter("content");
        String path = null;
        String type = null;
        String url;
        if(file == null){
            url = "";
            int i = seekTeacherService.insertItemBank(user,url,content);
            if (i == 1)
                return "上传成功";
            else return "false";
        }else{
            String fileName = file.getOriginalFilename();
            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            if (type != null) {
                if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
                    // 项目在容器中实际发布运行的根路径
                    String realPath = "/usr/lyjjImage/lyjj2/itemBank/";
//                    String realPath = "E://ssss/itemBank/";
                    // 自定义的文件名称
                    String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
                    // 设置存放图片文件的路径
                    path = realPath + trueFileName;
                    file.transferTo(new File(path));
                    int i = seekTeacherService.insertItemBank(user, "https://www.auriakj.com/lyjjImage/lyjj2/itemBank/" + trueFileName,content);
                    if (i == 1)
                        return "上传成功";
                    else return "false";
                } else {
                    System.out.println("不是我们想要的文件类型,请按要求重新上传");
                    return "error";
                }
            } else {
                System.out.println("文件类型为空");
                return "error";
            }
        }
    }

    @ApiOperation(value = "解答")
    @PostMapping(value = "/insertAnswer")
    @ResponseBody
    public synchronized String insertAnswer(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        request.setCharacterEncoding("UTF-8");
        String user = request.getParameter("userId");
        String itemBankId = request.getParameter("itemBankId");
        String content = request.getParameter("content");
        String path = null;
        String type = null;
        String url;
        if(file == null){
            url = "";
            int i = seekTeacherService.insertAnswer(user,itemBankId,content,url);
            if (i == 1)
                return "上传成功";
            else return "false";
        }else{
            String fileName = file.getOriginalFilename();
            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            if (type != null) {
                if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
                    // 项目在容器中实际发布运行的根路径
                    String realPath = "/usr/lyjjImage/lyjj2/answer/";
//                    String realPath = "E://ssss/answer/";
                    // 自定义的文件名称
                    String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
                    // 设置存放图片文件的路径
                    path = realPath + trueFileName;
                    file.transferTo(new File(path));
                    int i = seekTeacherService.insertAnswer(user,itemBankId,content,"https://www.auriakj.com/lyjjImage/lyjj2/answer/" + trueFileName);
                    if (i == 1)
                        return "上传成功";
                    else return "false";
                } else {
                    System.out.println("不是我们想要的文件类型,请按要求重新上传");
                    return "error";
                }
            } else {
                System.out.println("文件类型为空");
                return "error";
            }
        }


    }

    //反馈↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

    @ApiOperation(value = "查看老师对我的反馈")
    @PostMapping(value = "/findFeedBackList")
    @ResponseBody
    public ListExcution<List<FeedBackInfo>> findFeedBackList(Integer limit,Integer userId){
        List<FeedBackInfo> list = seekTeacherService.findFeedBackList(limit,userId);
        return new ListExcution<List<FeedBackInfo>>(list, ListEnum.SUCCESS);
    }

    @ApiOperation(value = "家长端   查看老师反馈的详情和回复")
    @PostMapping(value = "/findTeacherFeedBackDownStudentList1")
    @ResponseBody
    public ListExcution<FeedBackInfo> findTeacherFeedBackDownStudentList1(Integer teacherFeedBackId){
        FeedBackInfo list = seekTeacherService.findTeacherFeedBackDownStudentList1(teacherFeedBackId);
        return new ListExcution<FeedBackInfo>(list, ListEnum.SUCCESS);
    }

    @ApiOperation(value = "老师端   查看老师反馈的详情和回复")
    @PostMapping(value = "/findTeacherFeedBackDownStudentList")
    @ResponseBody
    public ListExcution<List<FeedBackInfo>> findTeacherFeedBackDownStudentList(Integer limit,Integer userId){
        List<FeedBackInfo> list = seekTeacherService.findTeacherFeedBackDownStudentList(limit,userId);
        return new ListExcution<List<FeedBackInfo>>(list, ListEnum.SUCCESS);
    }

    @ApiOperation(value = "老师反馈")
    @PostMapping(value = "/insertFeedBack")
    @ResponseBody
    public synchronized String insertFeedBack(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        request.setCharacterEncoding("UTF-8");
        String teaUserId = request.getParameter("teaUserId");
        String stdUserId = request.getParameter("stdUserId");
        String content = request.getParameter("content");
        String path = null;
        String type = null;
        String url;
        if(file == null){
            url = "";
            int i = seekTeacherService.insertFeedBack(teaUserId,stdUserId,content,url);
            if (i == 1)
                return "上传成功";
            else return "false";
        }else{
            String fileName = file.getOriginalFilename();
            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            if (type != null) {
                if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
                    // 项目在容器中实际发布运行的根路径
                    String realPath = "/usr/lyjjImage/lyjj2/feedBack1/";
//                    String realPath = "E://ssss/feedBack/";
                    // 自定义的文件名称
                    String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
                    // 设置存放图片文件的路径
                    path = realPath + trueFileName;
                    file.transferTo(new File(path));
                    int i = seekTeacherService.insertFeedBack(teaUserId,stdUserId,content,"https://www.auriakj.com/lyjjImage/lyjj2/feedBack1/" + trueFileName);
                    if (i == 1)
                        return "上传成功";
                    else return "false";
                } else {
                    System.out.println("不是我们想要的文件类型,请按要求重新上传");
                    return "error";
                }
            } else {
                System.out.println("文件类型为空");
                return "error";
            }
        }
    }

    @ApiOperation(value = "老师或家长回复老师反馈")
    @PostMapping(value = "/insertUserFeedBack")
    @ResponseBody
    public synchronized String insertUserFeedBack(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        request.setCharacterEncoding("UTF-8");
        String userId = request.getParameter("userId");
        String user = request.getParameter("type");
        String teacherFeedBackId = request.getParameter("teacherFeedBackId");
        String content = request.getParameter("content");
        String path = null;
        String type = null;
        String url;
        int a = 0;
        if(user.equals("student")){
            a = 1;
        }else{
            a = 2;
        }
        if(file == null){
            url = "";
            int i = seekTeacherService.insertStdFeedBack(userId,teacherFeedBackId,content,url,a);
            if (i == 1)
                return "上传成功";
            else return "false";
        }else{
            String fileName = file.getOriginalFilename();
            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            if (type != null) {
                if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
                    // 项目在容器中实际发布运行的根路径
                    String realPath = "/usr/lyjjImage/lyjj2/feedBack2/";
//                    String realPath = "E://ssss/feedBack/";
                    // 自定义的文件名称
                    String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
                    // 设置存放图片文件的路径
                    path = realPath + trueFileName;
                    file.transferTo(new File(path));
                    int i = seekTeacherService.insertStdFeedBack(userId,teacherFeedBackId,content,"https://www.auriakj.com/lyjjImage/lyjj2/feedBack2/" + trueFileName,a);
                    if (i == 1)
                        return "上传成功";
                    else return "false";
                } else {
                    System.out.println("不是我们想要的文件类型,请按要求重新上传");
                    return "error";
                }
            } else {
                System.out.println("文件类型为空");
                return "error";
            }
        }
    }


    @ApiOperation(value = "编辑简历多图上传")
    @PostMapping(value = "/uploadM")
    @ResponseBody
    public synchronized String uploadM(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        request.setCharacterEncoding("UTF-8");
        String teacherId = request.getParameter("teacherId");
        String num = request.getParameter("num");
        String path = null;
        String type = null;
        String url;
        if(file == null){
            url = "";
            if(num.equals("1")){  //一是案例
                int i = seekTeacherService.insertTeacherUrl2(teacherId,url);
                if (i == 1)
                    return "上传成功";
                else return "false";
            }else{  //2是介绍和教学经验
                int i = seekTeacherService.insertTeacherUrl(teacherId,url);
                if (i == 1)
                    return "上传成功";
                else return "false";
            }

        }else{
            String fileName = file.getOriginalFilename();
            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            if (type != null) {
                if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
                    // 项目在容器中实际发布运行的根路径
                    String realPath;
                    if(num.equals("1")){
                         realPath = "/usr/lyjjImage/lyjj2/teacherUrl2/";
//                         realPath = "E://ssss/teacherUrl2/";
                    }else{
                         realPath = "/usr/lyjjImage/lyjj2/teacherUrl/";
//                         realPath = "E://ssss/teacherUrl/";
                    }
//                    String realPath = "E://ssss/feedBack/";
                    // 自定义的文件名称
                    String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
                    // 设置存放图片文件的路径
                    path = realPath + trueFileName;
                    file.transferTo(new File(path));
                    if(num.equals("1")){
                        int i = seekTeacherService.insertTeacherUrl2(teacherId,"https://www.auriakj.com/lyjjImage/lyjj2/teacherUrl2/" + trueFileName);
                        if (i == 1)
                            return "上传成功";
                        else return "false";
                    }else{
                        int i = seekTeacherService.insertTeacherUrl(teacherId,"https://www.auriakj.com/lyjjImage/lyjj2/teacherUrl/" + trueFileName);
                        if (i == 1)
                            return "上传成功";
                        else return "false";
                    }
                } else {
                    System.out.println("不是我们想要的文件类型,请按要求重新上传");
                    return "error";
                }
            } else {
                System.out.println("文件类型为空");
                return "error";
            }
        }
    }

    @ApiOperation(value = "查看老师对应的用户")
    @PostMapping(value = "/findTeacherUser")
    @ResponseBody
    public ListExcution<List<StudentInfo>> findTeacherUser(Integer limit,Integer userId){
        List<StudentInfo> list = seekTeacherService.findTeacherUser(limit,userId);
        return new ListExcution<List<StudentInfo>>(list, ListEnum.SUCCESS);
    }

}
